//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    like: [],
    page: 1,
    last_page: 0,
    host: app.globalData.base_host,
    bottomText: '玩命加載中...'
  },
  onLoad: function() {
    const me = this;
    this.setData({
      page: 1,
      bottomText: '玩命加載中...'
    })
    if (app.globalData.openid) {
      this.getList(true);
    } else {
      app.getAuthKey().then(function (res) {
        me.getList(true);
      })
    }
  },
  onShow: function(){
    
  },
  onPullDownRefresh() {
    this.setData({
      page: 1,
      bottomText: '玩命加載中...'
    })
    this.getList(true);
  },
  onReachBottom() {
    if (this.data.page <= this.data.last_page) {
      this.getList()
    }else{
      this.setData({
        bottomText: '沒有更多了!'
      })
    }
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  imgTap(e) { //图片点击
    let url_arr = []
    const list = this.data.list;
    list.map(function(item,index){
      url_arr.push(app.globalData.base_host+item.url)
    })
    wx.previewImage({
      urls: url_arr,
      current: e.target.dataset.img
    })
  },
  imgLongTap(e) { //图片长按操作
    const {id,idx,img,open_id} = e.target.dataset
    const me = this
    wx.showActionSheet({
      itemList: ['删除','保存到本地'],
      itemColor: '#C13C39',
      success(res) {
        if (res.tapIndex === 0) {
          console.log(open_id,app.globalData.openId)
          if(app.globalData.openId != open_id){
            wx.showToast({
              title: '不可以删除别人的图片哦!',
              icon: 'none',
            })
          }else{
            me.delImg(id, idx)
          }
        }
        if(res.tapIndex === 1) {
          wx.getSetting({
            success(res) {
              if(res.authSetting['scope.writePhotosAlbum']){
                wx.showLoading({
                  title: '图片保存中...',
                })
                wx.downloadFile({
                  url: img,
                  success(res) {
                    if (res.statusCode === 200) {
                      wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success(res) {
                          wx.showToast({
                            title: '保存成功',
                          })
                        }
                      })
                    }
                  }
                })
              }else{
                wx.openSetting({
                  complete(res){
                    console.log(res)
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  getList: function(pullRefresh = false) { //获取图片列表
    const me = this;
    wx.request({
      url: app.globalData.host + 'img/get',
      method: 'post',
      data: {
        page: me.data.page,
        open_id: app.globalData.openId
      },
      success: function(res) {
        const last_page = res.data.meta.last_page
        let page = me.data.page
        if (last_page >= page) {
          page += 1
        }
        const newData = res.data.data
        const oldData = pullRefresh ? [] : me.data.list
        const lastData = oldData.concat(newData)
        me.setData({
          list: lastData,
          page: page,
          last_page: last_page
        })
      },
      complete: function(res) {
        wx.stopPullDownRefresh()
        me.getLike()
      }
    })
  },
  delImg: function(id, idx) { //图片删除操作
    const me = this
    wx.request({
      url: app.globalData.host + 'img/del',
      method: 'post',
      data: {
        id: id,
        idx: idx
      },
      success(res) {
        if (res.data.state === 'success') {
          wx.showToast({
            title: '刪除成功',
          })
          let list = me.data.list
          list.splice(idx, 1)
          me.setData({
            list: list
          })
        }
      }
    })
  },
  likeTap(e){ //点击 喜欢 事件
    const me = this,id = e.target.dataset.id,open_id = app.globalData.openId,like = this.data.like,
    idx = e.target.dataset.idx,list = this.data.list
    let flag = true
    if(like.indexOf(id) === -1 && flag){
      flag = false
      wx.request({
        url: app.globalData.host+'img/addLike',
        method: 'POST',
        data: {img_id: id,open_id: open_id},
        success(res) {
          like.push(id)
          me.setData({like: like})
          let newList = []
          list.map(function(item,index){
            if(index === idx){
              item.like_count += 1
              item.img_like = [{img_id:item.id}]
            }
            newList.push(item)
          })
          me.setData({list: newList})
          flag = true
        }
      })
    }else{
      wx.showToast({
        title: '已经点过喜欢了',
        icon: 'none'
      })
    }
  },
  getLike: function() {
    const me = this
    const open_id = app.globalData.openId
    wx.request({
      url: app.globalData.host+'img/getLike',
      method: 'POST',
      data: {
        open_id: open_id
      },success(res) {
        if(res.data.status === 'success'){
          me.setData({like: res.data.data})
        }
      }
    })
  }
})